#ifndef CHATMANAGER_H
#define CHATMANAGER_H

#include "accountmanager.h"
#include "account/accountbuddy.h"
#include "contact/contactabstractcontact.h"
#include "contact/contactprivatecontact.h"
#include "contact/contactgroupcontact.h"
#include "contact/contactmanager.h"
#include "converse/converseabstractconversation.h"
#include "converse/converseabstractmessage.h"
#include "converse/conversemessagegroup.h"
#include "converse/conversemanager.h"

#include <QObject>

class ChatManager : public QObject
{
    Q_OBJECT
public:
    explicit ChatManager(QObject *parent = 0);
    static ChatManager *instance(QObject *parent = 0);

    void doLogin(QString username, QString password);
    void doCreateNewAccount(QString phone, QString code);
    void doGetActiveCode(QString phone);
    void doLogout();
    void updatePrivateContactData(qint64 buddyId);
    void updateGroupContactData(qint64 groupId);
    void getHistoryMessageFromLocal(qint64 uid, qint64 buddyId);
    void sendMessage(QString message);

    qint64 activeUid() const { return m_activeUid; }
    qint64 activeFid() const { return m_activeFid; }

    const Converse::AbstractConversation* activeConversation() const {
        return m_activeConversation.data();
    }

signals:
    void logined(bool successed, qint64 uid);
    void logouted();

    void requestBuddiesFinished();
    void insertMessageGroup(Converse::MessageGroup *messageGroup);
    void messageReadFromLocal(Converse::AbstractMessage *message);
    void messageSend(Converse::AbstractMessage *message);
    void messageSent(Converse::AbstractMessage *message);
    void messageReceived(Converse::AbstractMessage *message);

private slots:
    void onLogined(bool successed, const QString &message, const QString &sessionKey);
    void onLogouted();

    void onRequestBuddiesFinished();
    void onMessageReadFromLocal(Converse::AbstractMessage *message);
    void onMessageReceived(Converse::AbstractMessage *message);
    void onMessageSent(Converse::AbstractMessage *message);
    void onMessageSend(Converse::AbstractMessage *message);
    void onDataInserted(int index, Converse::MessageGroup *messageGroup);

private:
    void init();

    qint64 m_activeUid;
    qint64 m_activeFid;


    QPointer<Contact::AbstractContact> m_activeContact;
    QPointer<Converse::AbstractConversation> m_activeConversation;

    static ChatManager *m_instance;

};

#endif // CHATMANAGER_H
